package org.summer.lawyer.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.ArticleComment;
import org.summer.lawyer.domain.bo.articlecomment.ArticleCommentBo;
import org.summer.lawyer.domain.vo.article.ArticleVo;
import org.summer.lawyer.domain.vo.articlecomment.ArticleCommentVo;
import org.summer.lawyer.domain.vo.wxuser.WxUserVo;
import org.summer.lawyer.mapper.ArticleCommentMapper;
import org.summer.lawyer.service.IArticleCommentService;
import org.summer.lawyer.service.IArticleService;
import org.summer.lawyer.service.IWxUserService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 文章评论Service业务层处理
 *
 * @author summer
 * @date 2024-06-27
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentMapper, ArticleComment>  implements IArticleCommentService {

    private final IWxUserService wxUserService;

    private final IArticleService articleService;

    /**
     * 查询文章评论
     *
     * @param id 主键
     * @return 文章评论
     */
    @Override
    public ArticleCommentVo selectArticleCommentVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询文章评论列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 文章评论分页列表
     */
    @Override
    public TableDataInfo<ArticleCommentVo> selectPageList(ArticleCommentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArticleComment> lqw = buildQueryWrapper(bo);
        Page<ArticleCommentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的文章评论列表
     *
     * @param bo 查询条件
     * @return 文章评论列表
     */
    @Override
    public List<ArticleCommentVo> selectArticleCommentVoList(ArticleCommentBo bo) {
        LambdaQueryWrapper<ArticleComment> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArticleComment> buildQueryWrapper(ArticleCommentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArticleComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getArticleId() != null, ArticleComment::getArticleId, bo.getArticleId());
        lqw.eq(StrUtil.isNotBlank(bo.getCategoryId()), ArticleComment::getCategoryId, bo.getCategoryId());
        lqw.like(StrUtil.isNotBlank(bo.getCategoryName()), ArticleComment::getCategoryName, bo.getCategoryName());
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), ArticleComment::getTitle, bo.getTitle());
        lqw.eq(StrUtil.isNotBlank(bo.getImageUrl()), ArticleComment::getImageUrl, bo.getImageUrl());
        lqw.eq(bo.getWxUserId() != null, ArticleComment::getWxUserId, bo.getWxUserId());
        lqw.like(StrUtil.isNotBlank(bo.getNickname()), ArticleComment::getNickname, bo.getNickname());
        lqw.eq(StrUtil.isNotBlank(bo.getCommentContent()), ArticleComment::getCommentContent, bo.getCommentContent());
        lqw.eq(bo.getCommentTime() != null, ArticleComment::getCommentTime, bo.getCommentTime());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), ArticleComment::getStatus, bo.getStatus());
        lqw.eq(bo.getReviewerId() != null, ArticleComment::getReviewerId, bo.getReviewerId());
        lqw.like(StrUtil.isNotBlank(bo.getReviewerName()), ArticleComment::getReviewerName, bo.getReviewerName());
        lqw.eq(bo.getReviewTime() != null, ArticleComment::getReviewTime, bo.getReviewTime());
        return lqw;
    }

    /**
     * 新增文章评论
     *
     * @param bo 文章评论
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ArticleCommentBo bo) {
        //是否已经评论过
//        if(checkArticleCommentUnique(bo)){
//            throw new ServiceException("你已经评论过了");
//        }
        ArticleComment add = MapstructUtils.convert(bo, ArticleComment.class);
        validEntityBeforeSave(add);
        //获取评论人信息
        WxUserVo wxUser = wxUserService.selectWxUserVoById(bo.getWxUserId());
        add.setNickname(wxUser.getNickname());
        add.setAvatar(wxUser.getAvatar());
        //获取文章信息
        ArticleVo articleVo = articleService.selectArticleVoById(bo.getArticleId());
        add.setCategoryId(articleVo.getCategoryId());
        add.setCategoryName(articleVo.getCategoryName());
        add.setTitle(articleVo.getTitle());
        add.setImageUrl(articleVo.getImageUrl());
        add.setCommentTime(DateUtil.date());
        add.setStatus("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文章评论
     *
     * @param bo 文章评论
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ArticleCommentBo bo) {
        ArticleComment update = MapstructUtils.convert(bo, ArticleComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ArticleComment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除文章评论信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean checkArticleCommentUnique(ArticleCommentBo bo) {
        return baseMapper.exists(new LambdaQueryWrapper<ArticleComment>().eq(ArticleComment::getWxUserId, bo.getWxUserId()).eq(ArticleComment::getArticleId, bo.getArticleId()));
    }
}
